package ac.il.technion.cs236369.hw5.core.servlets.instruments;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;

import ac.il.technion.cs236369.hw5.core.entities.Instrument;
import ac.il.technion.cs236369.hw5.core.entities.Role;

public enum InstrumentParameters {
	
	
	//TODO: Missing type safety check missing. Need to be added.
	INSTRUMENT_ID( "id") {
		public void saveParameterValue( Instrument instrument, HttpServletRequest request, EntityManager manager) {
			// There is no need to setup id, since it auto-generated.
		}
	},
	INSTRUMENT_TYPE( "type") {
		public void saveParameterValue(Instrument instrument, HttpServletRequest request, EntityManager manager ) {
			instrument.setType( request.getParameter( this.getName( )));
		}
	},
	INSTRUMENT_TIME_SLOT( "timeSlot") {
		public void saveParameterValue( Instrument instrument, HttpServletRequest request, EntityManager manager) {
			instrument.setTimeSlot( Integer.valueOf( request.getParameter( this.getName())));
		}
	},
	INSTRUMENT_DESCRIPTION( "description") {
		public void saveParameterValue( Instrument instrument, HttpServletRequest request, EntityManager manager) {
			instrument.setDescription( request.getParameter( this.getName( )));
		}
	},
	INSTRUMENT_PRICE( "usagePrice") {
		public void saveParameterValue( Instrument instrument, HttpServletRequest request, EntityManager manager) {
			instrument.setUsagePrice( Double.valueOf( request.getParameter(this.getName( ))));
		}
	},
	INSTRUMENT_ROLE( "role") {
		public void saveParameterValue( Instrument instrument, HttpServletRequest request, EntityManager manager) {
			Role role = manager.find(Role.class, request.getParameter(getName( )));
			instrument.setPermission(role);
		}
	};
	
	private static final Map<String, InstrumentParameters> map = new HashMap<String, InstrumentParameters>( );
	
	static {
		for( InstrumentParameters $ : InstrumentParameters.values( )) {
			map.put( $.getName( ).toUpperCase( ), $);
		}
	};
	
	private String name;
	
	private InstrumentParameters( String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}

	public InstrumentParameters fromString( String name) {
		return map.get(name);
	}
	
	abstract public void saveParameterValue( Instrument instrument, HttpServletRequest request, EntityManager manager);
}
